﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Common.Constants;
using Common.Helper;
using Entities;

namespace DataAccess
{
    public class DepartmentDataAccess : IDepartmentDataAccess
    {
        public static DepartmentDataAccess Instance { get; private set; }
        static DepartmentDataAccess(){Instance = new DepartmentDataAccess();}

        public int Insert(Department department)
        {
            if (department == null) return -1;
            var colParameter = new Collection<SqlParameter>
               {
                   new SqlParameter("DirectorateID",department.DirectorateId),
                   new SqlParameter("ContactID",department.ContactId),
                   new SqlParameter("Name",department.Name),
                   new SqlParameter("ShortDesc",department.ShortDesc),
                   new SqlParameter("FullDesc",department.FullDesc),
                   new SqlParameter("BusinessID",department.BusinessId),
                   new SqlParameter("AddressLine1",department.AddressLine1),
                   new SqlParameter("AddressLine2",department.AddressLine2),
                   new SqlParameter("AddressLine3",department.AddressLine3),
                   new SqlParameter("AddressID",department.AddressId),
                   new SqlParameter("PhoneNumber",department.PhoneNumber),
                   new SqlParameter("Fax",department.Fax),
                   new SqlParameter("Email",department.Email),
                   new SqlParameter("WebAddress",department.WebAddress),
                   new SqlParameter("IsActive",true)
               };
            var result = SqlHelper.ExecuteNonQuery(ProcedureStores.InsertDepartment,colParameter);
            return result;
        }

        public int Update(Department department)
        {
            if (department == null)
            {
                return -1;
            }
            var colParameter = new Collection<SqlParameter>
               {
                   new SqlParameter("DepartmentID",department.DepartmentId),
                   new SqlParameter("ContactID",department.ContactId),
                   new SqlParameter("Name",department.Name),
                   new SqlParameter("ShortDesc",department.ShortDesc),
                   new SqlParameter("FullDesc",department.FullDesc),
                   new SqlParameter("BusinessID",department.BusinessId),
                   new SqlParameter("AddressLine1",department.AddressLine1),
                   new SqlParameter("AddressLine2",department.AddressLine2),
                   new SqlParameter("AddressLine3",department.AddressLine3),
                   new SqlParameter("AddressID",department.AddressId),
                   new SqlParameter("PhoneNumber",department.PhoneNumber),
                   new SqlParameter("Fax",department.Fax),
                   new SqlParameter("Email",department.Email),
                   new SqlParameter("WebAddress",department.WebAddress)
               };
            var result = SqlHelper.ExecuteNonQuery(ProcedureStores.UpdateDepartment, colParameter);
            return result;
        }

        public IDataReader GetAll(Guid directorateId,bool include)
        {
            var colParameter = new Collection<SqlParameter>
                   {
                       new SqlParameter("DirectorateID",directorateId),
                       new SqlParameter("Include",include)
                   };
            var dataReader = SqlHelper.ExecuteReader(ProcedureStores.GetAllDepartment, colParameter);
            return dataReader;
        }

        public int ChangeStatus(Guid departmentId,bool include)
        {
            if (departmentId == Guid.Empty)
            {
                return -1;
            }
            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("DepartmentID",departmentId),
                                       new SqlParameter("Isactive",include)
                                   };
            var result = SqlHelper.ExecuteNonQuery(ProcedureStores.ChangeStatusDepartment, colParameter);
            return result;
        }

        public IDataReader GetById(Guid departmentId)
        {
            if(departmentId == Guid.Empty)
            {
                return null;
            }
            var colParameter = new Collection<SqlParameter>
               {
                   new SqlParameter("DepartmentID", departmentId)
               };
            var dataReader = SqlHelper.ExecuteReader(ProcedureStores.GetByIdDepartment, colParameter);
            return dataReader;
        }

        public IDataReader GetDifferentName(Guid departmentId,string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                return null;
            }
            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("DepartmentID", departmentId),
                                       new SqlParameter("Name", name)
                                   };
            var dataReader = SqlHelper.ExecuteReader(ProcedureStores.GetDifferentNameDepartment, colParameter);
            return dataReader;
        }
    }
}
